package com.kysd.uploadfile.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kysd.base.ApiModel;
import com.kysd.base.BaseController;
import com.kysd.base.Common;
import com.kysd.uploadfile.redis.RedisUtil;

/**
 * 缓存文件类型
 * @author liangjinquan
 *
 */
@RestController
@RequestMapping(value = "/uploadtype")
public class UploadTypeController extends BaseController {

	@Autowired
	private RedisUtil redisUtil;
	@Value("${maxFileLength}")
	private Long maxFileLength;
	/**
	 * 新增修改
	 * @param key
	 * @param length
	 * @return
	 */
	@RequestMapping("addOrUpdateFileType")
	@SuppressWarnings("all")
	public ApiModel addOrUpdateFileType(String key, Long length) {
		if(StringUtils.isEmpty(key)) {
			return FAIL("参数不能为空"); 
		}
		if(null == length || length.compareTo(0l) <= 0) {
			return FAIL("长度不能为空");
		}
		if(length.compareTo(maxFileLength) > 0) {
			return FAIL("长度不能超过101400字节(100M)");
		}
		
		boolean b = redisUtil.hset(Common.FILE_TYPE_KEY, key, length);
		if(b) {
			return SUCCESS();
		}else {
			return FAIL();
		}
	}
	
	/**
	 * 删除
	 * @param key
	 * @return
	 */
	@RequestMapping("delFileType")
	@SuppressWarnings("all")
	public ApiModel delFileType(String key) {
		redisUtil.hdel(Common.FILE_TYPE_KEY, key);
		return SUCCESS();
	}
	
	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("getFileType")
	@SuppressWarnings("all")
	public ApiModel getFileType() {
		Map<Object, Object> map = redisUtil.hgetall(Common.FILE_TYPE_KEY);
		return SUCCESS(map);
	}
}
